package com.handels.core;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


public class GALogApplication{
	
    private static GALogApplication instance;
    private PrintWriter writer;
    private String fileName;
    
    public GALogApplication()   
    {
    	System.out.println("[GALogApplication] Creating AGLogMigration instance...");
        try
        {
            TimeZone tz = TimeZone.getTimeZone("GMT-3");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
            dateFormat.setTimeZone(tz);
            this.fileName = HAApplication.getInstance().getRealPath() + "WEB-INF" + File.separator + "logs" + File.separator + "GALogApplication_" + dateFormat.format(HAApplication.getInstance().getStartDate()) + ".txt";
            System.out.println("#fileName= "+fileName);
            writer = new PrintWriter(new FileOutputStream(this.fileName, false));
            this.log("[GALogApplication] Creating log file: '" + this.fileName + "'");

        } catch (Exception e)
        {
            this.log("[GALogApplication] ERROR: " + e.getClass().getName() + ": " + e.getMessage());
        }
    }
   
    public static GALogApplication getInstance()
    {
        if(instance == null)
            instance = new GALogApplication();
        return instance;
    }
   
    public synchronized void log(String message)
    {
        TimeZone tz = TimeZone.getTimeZone("GMT-3");
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        sdf.setTimeZone(tz);
           
        String logMsg = sdf.format(new Date());
        logMsg += " " + message + "\r\n";
       
        writer.write(logMsg);
        writer.flush();
    }
}